from odoo import fields, models
from odoo.tools import float_is_zero, float_compare
from odoo.exceptions import UserError


class StockMoveLine(models.Model):
    _inherit = 'stock.move.line'

    # def _action_done(self):
    #     res = super()._action_done()
    #     for rec in self.filtered(lambda x: x.state == 'done'):
    #         product_id = rec.product_id
    #         product_rounding = product_id.uom_id.rounding
    #         lock_stock_product_ids = rec.s_lock_ids.filtered(lambda x: x.lock_type == 'stock')
    #         if float_is_zero(sum(lock_stock_product_ids.mapped('qty')), precision_rounding=product_rounding):
    #             continue
    #         else:
    #             if rec.location_id.usage != 'internal':
    #                 continue
    #
    #             # 生产前库位
    #             pre_production_location_id = self.env['stock.location'].search([('barcode', '=', 'WH-PREPRODUCTION')], limit=1)
    #             pre_production_location_ids = pre_production_location_id + pre_production_location_id.child_ids
    #             if rec.location_dest_id.usage == 'internal' and rec.location_dest_id in pre_production_location_ids:
    #                 continue
    #
    #             picking_id = rec.move_id.picking_id
    #             production_ids = picking_id.s_mrp_production_ids
    #             sale_ids = production_ids.mapped('s_sale_order_line_ids.order_id')
    #
    #             product_id = rec.product_id
    #             if not sale_ids:
    #                 if float_compare(product_id.s_actual_quantity, product_id.s_lock_qty, precision_rounding=product_rounding) == -1:
    #                     raise UserError('Product %s, Inventory quantity is less than the locked quantity, please unlock first')
    #
    #             else:
    #                 ...
    #
    #     return res
